package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
    DBManager dbManager = DBManager.getInstance();
    JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();

    /**
     * 添加
     * @param guestbook
     * @return
     */
    @Override
    public int add(Guestbook guestbook) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        int a = 0;
        try {
            a = guestbookDao.add(guestbook);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Guestbook query(Integer id) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        Guestbook guestbook = null;
        try {
            guestbook = guestbookDao.query(id);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return guestbook;
    }

    /**
     * 模糊查询
     * @param name
     * @return
     */
    @Override
    public List<Object> mohuquery(String name) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        List<Object> list = null;
        try {
            list = guestbookDao.mohuquery(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        int a = 0;
        try {
            a = guestbookDao.delete(id);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }

    /**
     * 修改
     * @param guestbook
     * @return
     */
    @Override
    public int update(Guestbook guestbook) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        int a = 0;
        try {
            a = guestbookDao.update(guestbook);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Object> allquery() {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        List<Object> list = null;
        try {
            list = guestbookDao.allquery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
